package cn.yeyiu.netonlinevideo.parser.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.netonlinevideo.viewmodel.DataListViewModel;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;
import cn.yeyiu.util.bean.VideoInfo;

public class RiYeThemeParser extends RiYeParser{

    Map<String, String> typeMap = new HashMap<>();

    public RiYeThemeParser(SourceData source) {
        super(source);
    }

    @Override
    public List<TypeData> getTypeList() {
        List<TypeData> types = new ArrayList<>();

        JSONObject data = new JSONObject();
        for(int i=1;;i++){
            data.put("page", i);
            String html = NetUtil.postByUrl(source.url + "theme/index", data, false, header);
            JSONObject json = getJson(html);
            JSONArray array = json.getJSONObject("data").getJSONArray("list");
            if(array==null||array.size()==0){
                break;
            }
            for (int j = 0; j < array.size(); ++j) {
                String id = array.getJSONObject(j).getString("id");
                String title = array.getJSONObject(j).getString("title");
                types.add(new TypeData(title));
                typeMap.put(title, id);
            }
        }
        return types;
    }

    @Override
    public Object getBaseData(String type, int pageIndex) {
        JSONObject data = new JSONObject();
        data.put("theme_id", typeMap.get(type));
        data.put("page", pageIndex+1);
        String html = NetUtil.postByUrl(source.url + "theme/movie_list", data, false, header);
        return getJson(html);
    }
}
